package com.zzq.my.jetpacks.jectpackapp.ui.act


import android.util.Log
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.aisier.architecture.util.toast
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle
import com.zzq.my.jetpacks.jectpackapp.adapter.BaseBannerMenuAdapter
import com.zzq.my.jetpacks.jectpackapp.adapter.SimpleAdapter
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityBannerviewpagerBinding
import com.zzq.my.jetpacks.jectpackapp.utils.ListUtilGroup
import com.zzq.my.jetpacks.util.goActivity


/**
 * 描述：bannerviewpager
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class BannerviewpagerAct :
    BaseActivity(com.zzq.my.jetpacks.jectpackapp.R.layout.activity_bannerviewpager) {
    private val vb by viewBinding(ActivityBannerviewpagerBinding::bind)
    private lateinit var mViewPager: BannerViewPager<String>
    private lateinit var mViewPager2: BannerViewPager<List<String>>
    private lateinit var mBaseBannerMenuAdapter: BaseBannerMenuAdapter
    private val dataList: MutableList<String> = mutableListOf()

    /**
     * 初始化数据
     */
    override fun initData() {
        this.setupViewPager()
        this.setMenuVp()
    }
//    private fun setupRoundRectIndicator() {
//
//        val normalWidth: Int = getNormalWidth()
//        mViewPager.setIndicatorStyle(IndicatorStyle.ROUND_RECT)
//            .setIndicatorSliderGap(BannerUtils.dp2px(4))
//            .setIndicatorSlideMode(mSlideMode)
//            .setIndicatorHeight(resources.getDimensionPixelOffset(com.zzq.my.jetpacks.jectpackapp.R.dimen.dp_4))
//            .setIndicatorSliderColor(
//                getColor(com.zzq.my.jetpacks.jectpackapp.R.color.red_normal_color),
//                getColor(com.zzq.my.jetpacks.jectpackapp.R.color.red_checked_color)
//            )
//            .setIndicatorSliderWidth(normalWidth, checkedWidth).refreshData(getPicList(4))
//    }

    private fun setupViewPager() {
        val checkedWidth =
            resources.getDimensionPixelOffset(com.zzq.my.jetpacks.jectpackapp.R.dimen.dp_10)
        val normalWidth: Int =
            resources.getDimensionPixelOffset(com.zzq.my.jetpacks.jectpackapp.R.dimen.dp_4)


        mViewPager = findViewById(com.zzq.my.jetpacks.jectpackapp.R.id.banner_view)
        mViewPager.apply {
            //todo 通过setIndicatorView(IIndicator)替换内部指示器
            setIndicatorView(vb.indicatorView)
            setInterval(2000)//自动轮播时间间隔
            setCanLoop(true)//是否开启循环
            setAutoPlay(true)//是否开启自动轮播
            setIndicatorStyle(IndicatorStyle.ROUND_RECT)
            setIndicatorSlideMode(IndicatorSlideMode.SMOOTH)
            setIndicatorSliderWidth(normalWidth, checkedWidth)
            setIndicatorHeight(normalWidth)
            registerLifecycleObserver(lifecycle)
            //todo 点击事件
//            setOnPageClickListener { clickedView, position ->
//                Log.d(TAG, "setupViewPager: $position")
//                toast("position$position")
//            }
           registerOnPageChangeCallback(object:OnPageChangeCallback(){
               override fun onPageSelected(position: Int) {
                   super.onPageSelected(position)
                   Log.d(TAG, "registerOnPageChangeCallback: $position")
               }
           })
            adapter = SimpleAdapter()
        }.create()
        val data = listOf(
            "http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233",
            "http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233",
            "http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233",
            "http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233",
            "http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233",
            "https://cs-ysb-core.51ydmw.com/core2//images/wx/bg_my@2x.png"
        )
        //todo 点击事件
        mViewPager.setOnPageClickListener { clickedView, position ->
//           goActivity<SwipeDelMenuLayoutAct>()//侧滑控件设置

            goActivity<Linear_layout_addView_Activity>()
        }
        mViewPager.refreshData(data)
    }

    private fun setMenuVp() {
        dataList.apply {
            add("商户管理")
            add("代理管理")
            add("机具管理")
            add("交易查询")

            add("扩展代理")
            add("政策模版")
            add("政策模版2")
            add("政策模版3")

            add("政策模版4")
            add("政策模版5")

        }
        val checkedWidth =
            resources.getDimensionPixelOffset(com.zzq.my.jetpacks.jectpackapp.R.dimen.dp_10)
        val normalWidth: Int =
            resources.getDimensionPixelOffset(com.zzq.my.jetpacks.jectpackapp.R.dimen.dp_4)
        mViewPager2 = findViewById(com.zzq.my.jetpacks.jectpackapp.R.id.banner_view2)
        mViewPager2.apply {
            //todo 通过setIndicatorView(IIndicator)替换内部指示器
            setIndicatorView(vb.indicatorView2)
//            setIndicatorMargin(0,this.dp2px(50F),0,0)
            setInterval(2000)//自动轮播时间间隔
            setCanLoop(false)//是否开启循环
            setAutoPlay(false)//是否开启自动轮播
            setIndicatorStyle(IndicatorStyle.ROUND_RECT)
            setIndicatorSlideMode(IndicatorSlideMode.SCALE)
            setIndicatorSliderWidth(normalWidth, checkedWidth)
            setIndicatorHeight(normalWidth)
            registerLifecycleObserver(lifecycle)
            //todo 点击事件
//            setOnPageClickListener { clickedView, position ->
//                showMsg("position$position")
//                val rv_menulist = clickedView.findViewById<RecyclerView>(R.id.rv_menulist)
//                rv_menulist.setOnClickListener {
//                    showMsg("position$position")
//                }
//            }
            val menuAdapter = BaseBannerMenuAdapter()
            //注册监听事件1
//            menuAdapter.registerListener {
//                onItemClick { baseQuickAdapter, _, position ->
//                    toast(baseQuickAdapter.getItem(position).toString())
//                }
//            }
            //注册监听事件2
//            menuAdapter.setOnItemClickListener(object :BaseBannerMenuAdapter.OnItemClickCallBack{
//                override fun onItemClick(
//                    adapter: BaseQuickAdapter<*, *>,
//                    view: View,
//                    position: Int
//                ) {
//                    toast(position.toString())
//                    toast(adapter.getItem(position).toString())
//                }
//            })
//注册监听事件3
            menuAdapter.setOnItemClickListenerDsl { adapter, view, position ->
                toast(adapter.getItem(position).toString())
            }
            adapter = menuAdapter
        }.create()
        val dataList = ListUtilGroup.fixedGrouping(dataList, 8)
        mViewPager2.refreshData(dataList)
    }

    /**
     * 初始化操作
     */
    override fun initView() {


    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {

    }


}